package ftp.action;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import fpt.util.FTPUtil;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class DownloadOverwriteFileAdapter extends BaseAdapter implements OnClickListener{
	 FTPFile[] list = new FTPFile[1];
     List<FTPFile> listCheck = new ArrayList<FTPFile>();
     DownloadActivity context;
     RelativeLayout flag=null;
     Bundle language;
     String duoi="";
     
     public Bundle getLanguage() {
             return language;
     }

     public void setLanguage(Bundle language) {
             this.language = language;
     }

     public DownloadOverwriteFileAdapter() {
             
     }

     public FTPFile[] getList() {
             return list;
     }


     public void setList(FTPFile[] list) {
             this.list = list;
     }


     public DownloadOverwriteFileAdapter(FTPFile[] list, DownloadActivity context,Bundle language) {
             super();
             this.list = list;
             this.context = context;
             this.language=language;
     }

     @Override
     public int getCount() {

             return list.length;
     }

     @Override
     public Object getItem(int arg0) {

             return list[arg0];
     }

     @Override
     public long getItemId(int arg0) {

             return 0;
     }

     @Override
     public View getView(final int position, View convertView, ViewGroup parent) {
    	 LayoutInflater inflater = (LayoutInflater) context
         .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
         convertView = inflater.inflate(R.layout.downloadoverwritefile, null);
         
         final FTPFile file = list[position];
         final CheckBox checkBox = (CheckBox) convertView
         .findViewById(R.id.checkFile);

         final TextView txtName = (TextView) convertView
         .findViewById(R.id.txtName);
         final TextView txtSize = (TextView) convertView
         .findViewById(R.id.txtSize);
         final RelativeLayout linerFile = (RelativeLayout) convertView
         .findViewById(R.id.linerFile);
         
         txtName.setText(file.getName());
         if (file.isFile() ) {
                 txtName.setTextColor(Color.parseColor("#FFFFFF"));
                 txtSize.setText(file.getSize() + " bytes");
         } else if (file.isDirectory()) {
                 txtName.setTextColor(Color.parseColor("#FFFF00"));
                 txtSize.setText("");
         }

         // System.out.println(position + " - " + file.getName());
         int co = 0;
         for (int i = 0; i < listCheck.size(); i++) {
                 if (file.getName().equals(listCheck.get(i).getName())) {
                         checkBox.setChecked(true);
                         co = 1;
                 }
         }
         if (checkBox.isChecked()) {
                 if (co == 0) {
                         checkBox.setChecked(false);
                 }
         }

         checkBox.setOnClickListener(new View.OnClickListener() {
                 @Override
                 public void onClick(View arg0) {
                         if (checkBox.isChecked()) {
                                 listCheck.add(file);
                         } else {
                                 for (int i = 0; i < listCheck.size(); i++) {
                                         if (file.getName().equals(listCheck.get(i).getName())) {
                                                 listCheck.remove(i);
                                         }
                                 }

                         }

                 }
         });
         linerFile.setOnTouchListener(new View.OnTouchListener() {

                 @Override
                 public boolean onTouch(View v, MotionEvent event) {
                         if(flag!=null){
                                 flag.setBackgroundColor(Color.parseColor("#000000"));
                         }

                         if (event.getAction() == MotionEvent.ACTION_DOWN) {
                                 linerFile.setBackgroundColor(Color.parseColor("#ff6600"));
                         } else if (event.getAction() == MotionEvent.ACTION_UP) {
                                 linerFile.setBackgroundColor(Color.parseColor("#ff6600"));
                         }
                         flag=linerFile;
                         return false;
                 }
         });

         linerFile.setOnClickListener(new View.OnClickListener() {

                 @Override
                 public void onClick(View v) {
                         context.loadLocation(file);
                 }
         });

         linerFile.setOnLongClickListener(new View.OnLongClickListener() {

                 @Override
                 public boolean onLongClick(View v) {

                         @SuppressWarnings("static-access")
                         LayoutInflater inflater = (LayoutInflater) context.getApplicationContext().getSystemService(context.LAYOUT_INFLATER_SERVICE);
                         View viewDelete = inflater.inflate(R.layout.deletedetail, null);

                         final TextView fname = (TextView)viewDelete.findViewById(R.id.idfilename);
                         final TextView ftype = (TextView)viewDelete.findViewById(R.id.idtypefile);
                         final TextView fsize = (TextView)viewDelete.findViewById(R.id.idsize);
                         final TextView fmodify = (TextView)viewDelete.findViewById(R.id.idmodifydate);

                         fname.setText(language.getString("filename")+":");
                         ftype.setText(language.getString("filetype")+":");
                         fsize.setText(language.getString("filesize")+":");
                         fmodify.setText(language.getString("lastmodify")+":");

                         final TextView vfname = (TextView)viewDelete.findViewById(R.id.valuenamefile);
                         final TextView vftype = (TextView)viewDelete.findViewById(R.id.valuetypefile);
                         final TextView vfSize = (TextView)viewDelete.findViewById(R.id.valuesize);
                         final TextView vfModify = (TextView)viewDelete.findViewById(R.id.valuemodifydate);

                         vfname.setText(file.getName().toString());
                         if(file.isDirectory()){
                                 vftype.setText(language.getString("folder"));
                         }else if(file.isFile()){
                                 vftype.setText(language.getString("file"));
                         }

                         if(file.isFile()){
                                 vfSize.setText(file.getSize() + " bytes");
                         }else if(file.isDirectory()){
                                 vfSize.setText(FTPUtil.totalSize(file,context.tempPath) + " bytes");
                         }

                         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                         for (FTPFile file1 : list) {
                                 file1 = list[position];
                                 Date date = file1.getTimestamp().getTime();
                                 String s = sdf.format(date);
                                 vfModify.setText(s);
                         }

                         AlertDialog.Builder builderDetail = new AlertDialog.Builder(context);
                         builderDetail.setTitle(file.getName().toString()+" "+language.getString("properties"));
                         builderDetail.setView(viewDelete);

                         builderDetail.setNegativeButton(language.getString("cancel"), new DialogInterface.OnClickListener() {

                                 @Override
                                 public void onClick(DialogInterface dialog, int which) {

                                 }
                         });
                         AlertDialog al = builderDetail.create();
                         al.show();

                         return false;
                 }
         });

             return convertView;
     }

     public List<FTPFile> getListCheck() {
             return listCheck;
     }

     public void setListCheck(List<FTPFile> listCheck) {
             this.listCheck = listCheck;
     }


     @Override
     public void onClick(View v) {
     }

     public void delete(String path) {
             FTPClient client = new FTPClient();
             try {
                     client.deleteFile(path);
             } catch (IOException e) {
                     e.printStackTrace();
             }
     }

}
